﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using AnimalCare.Model;
using AnimalCare.Service;

namespace AnimalCare.View
{
    public partial class FormEmployee : DevExpress.XtraEditors.XtraForm
    {
        public FormEmployee()
        {
            InitializeComponent();
            refreshGrid();
            InitGrid();
        }

        void InitGrid()
        {
        }
        protected void refreshGrid()
        {
            AnimalCareDBEntities entities;
            entities = new AnimalCareDBEntities();
            gridEmployee.DataSource = entities.Employees;
            gridEmployee.Refresh();
        }

        private void InsertEmployee_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(textNameEmployee.Text))
            {
                EmployeeService.Insert(textNameEmployee.Text, textphoneEmployee.Text, textInfoEmployee.Text);
                refreshGrid();
                textNameEmployee.Text = "";
                textphoneEmployee.Text = "";
                textInfoEmployee.Text = "";
            }
            else
            {
                ValidateService.validateEmpty(textNameEmployee, errorProvider1, "Chưa nhập tên nhân viên!!!");
            }
        }

        private void DeleteMenuItem_Click(object sender, EventArgs e)
        {
            int[] selectedRowsIndex = gridViewEmployee.GetSelectedRows();
            if (selectedRowsIndex.Length > 0)
            {
                int indexRow = selectedRowsIndex[0];
                Employee cl = (Employee)gridViewEmployee.GetRow(indexRow);
                EmployeeService.Delete(cl.employee_id);
                refreshGrid();
            }
        }

        private void UpdateMenuItem_Click(object sender, EventArgs e)
        {
            int[] selectedRowsIndex = gridViewEmployee.GetSelectedRows();
            if (selectedRowsIndex.Length > 0)
            {
                int indexRow = selectedRowsIndex[0];
                Employee cl = (Employee)gridViewEmployee.GetRow(indexRow);
                EmployeeService.Update(cl);
                refreshGrid();
            }
        }

        private void textNameEmployee_TextChanged(object sender, EventArgs e)
        {
            ValidateService.validateEmpty(textNameEmployee, errorProvider1, "Chưa nhập tên nhân viên!!!");
        }

        private void textphoneEmployee_TextChanged(object sender, EventArgs e)
        {
            ValidateService.validateNumber(textphoneEmployee, errorProvider1, "Chỉ được nhập số!!!");
        }
    }
}
